package com.icss.th.question.facade.mongo;

import java.util.UUID;

import com.icss.th.common.exception.ThException;
import com.icss.th.common.util.DateUtil;
import com.icss.th.db.MongoConnection;
import com.icss.th.question.facade.IOptionFacade;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;

public class OptionFacadeMongo implements IOptionFacade 
{	
	@Override
	public void saveExplain(String content, String examId, String ip) throws ThException 
	{
		DBCollection collection = MongoConnection.getCollection("userexplain");
		if (collection == null) return;
		
		BasicDBObject obj = new BasicDBObject();
		
		try
		{
			obj.append("_id", UUID.randomUUID().toString());
			obj.append("exam_id", Integer.valueOf(examId));
			obj.append("user_ip", ip);
			obj.append("content", content);
			obj.append("created_time", DateUtil.getCurrentDate());
			collection.save(obj);
		}
		catch (Exception e)
		{
			throw new ThException(e.getMessage(), e);
		}
	}

	@Override
	public void saveCorrect(String content, String examId, String optionType, String ip) throws ThException
	{
		DBCollection collection = MongoConnection.getCollection("corrent");
		if (collection == null) return;
		
		BasicDBObject obj = new BasicDBObject();
		
		try
		{
			obj.append("_id", UUID.randomUUID().toString());
			obj.append("EXAM_ID", Integer.valueOf(examId));
			obj.append("USER_IP", ip);
			obj.append("CONTENT", content);
			obj.append("OPTION_TYPE", optionType);
			obj.append("CREATED_TIME", DateUtil.getCurrentDate());
			collection.save(obj);
		}
		catch (Exception e)
		{
			throw new ThException(e.getMessage(), e);
		}
	}
}
